package com.cq.xjms.web.actions.sys;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cq.xjms.model.Clazz;
import com.cq.xjms.model.Course;
import com.cq.xjms.model.Institute;
import com.cq.xjms.model.Teacher;
import com.cq.xjms.service.SysManager;
import com.cq.xjms.service.TeacherManager;
import com.cq.xjms.web.actions.BaseAction;

public class TeacherAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(TeacherAction.class);
	private TeacherManager teacherManager;
	private Teacher teacher;
	private String type;
	private List list;
	private List<Course> courses;
	private List<Teacher> teachers;
	private SysManager sysManager;
	private Map<String,Object> params = new HashMap<String,Object>();
	
	public String addTeacher(){
		return SUCCESS;
	}
	public String saveTeacher(){
		
		try{
			params.put("teacherNo",teacher.getTeacherNo());
			teachers = sysManager.find(Teacher.TEACHER_QUERY,params);
			if(teachers==null || teachers.size()==0){
					sysManager.save(teacher);
					type="add";
			}else{
			     list = new ArrayList();
				 list.add("此老师编号已经存在！");
				 this.setActionErrors(list);
				
			}	
			
			
			
		}catch(Exception e){
			log.error("save teacher failed",e);
			return INPUT;
		}
		teacher = null;
		return SUCCESS;
	}
	/*public String getClazz(){
		return SUCCESS;
	}
	*/
	
	public String listTeacher(){
		LOG.info(teacher);
		try{
		if(teacher!=null){
			if(StringUtils.isNotEmpty(teacher.getName()))
				params.put("name", teacher.getName());
			if(StringUtils.isNotEmpty(teacher.getTeacherNo()))
				params.put("teacherNo", teacher.getTeacherNo());
			if(StringUtils.isNotEmpty(teacher.getEmail()))
				params.put("email", teacher.getEmail());
		}
		totalRows = teacherManager.countTeacher(params);
		teachers = teacherManager.queryTeachers(params,getLimit());
		}catch(Exception e){
			LOG.error("query teacher failed", e);
		}
		return SUCCESS;
	}
	public String editTeacher(){
		try{
			params.put("id", teacher.getId());
		    teachers = sysManager.find(Teacher.TEACHER_QUERYBYID,params);
		    if(teachers!=null && teachers.size()>0){
		    	teacher = teachers.get(0);
		    }
		}catch(Exception e){
			log.error("",e);
		}
		return SUCCESS;
	}
	public String editTeacherSave(){
		
		try{
			log.info(teacher.getId());
			sysManager.saveOrUpdate(teacher);
		
		}catch(Exception e){
			log.error("update clazz failed !",e);
			return INPUT;
		}
		
		return SUCCESS;
	}

	public String deleteTeacher(){
		sysManager.remove(Teacher.class, teacher.getId());
		return SUCCESS;
	}
	public static Logger getLog() {
		return log;
	}
	public static void setLog(Logger log) {
		TeacherAction.log = log;
	}
	public TeacherManager getTeacherManager() {
		return teacherManager;
	}
	public void setTeacherManager(TeacherManager teacherManager) {
		this.teacherManager = teacherManager;
	}
	
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public List<Course> getCourses() {
		return courses;
	}
	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}
	
	public List<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}
	public SysManager getSysManager() {
		return sysManager;
	}
	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}
	public Map<String, Object> getParams() {
		return params;
	}
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	
	
	
}
